home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / daten / ispell / source / amiga.c next >
C/C++ Source or Header  |  1995-07-03  |  7KB  |  317 lines

  1. static char RCS_Amiga_ID[]=
  2.   "$Id: amiga.c 1.5 1995/07/03 23:54:58 jskov Exp $";
  3.  
  4. /*
  5.  * $Log: amiga.c $
  6.  * Revision 1.5  1995/07/03  23:54:58  jskov
  7.  * Removed #include "version.h" - version strings are now in an object
  8.  *
  9.  * Revision 1.4  1995/06/30  13:13:00  jskov
  10.  * RCS version string is now in the object code for easy id of patch revision
  11.  *
  12.  * Revision 1.3  1995/06/30  12:40:21  jskov
  13.  * Added initialization of ctoken
  14.  *
  15.  * Revision 1.2  1995/06/30  10:10:16  jskov
  16.  * Added "ACCEPT" command - accepts word for the rest of the session.
  17.  *
  18.  * Revision 1.1  1995/06/30  00:03:56  jskov
  19.  * Initial revision
  20.  *
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "config.h"
  26. #include "ispell.h"
  27. #include "proto.h"
  28. #include <dos/dos.h>
  29. #include <proto/exec.h>
  30.  
  31.  
  32. extern char * Version_ID[];
  33.  
  34. struct rexxCommandList rcl[] =
  35. {
  36.   {"add", &rexxadd},
  37.   {"quickadd", &rexxquickadd},
  38.   {"accept", &rexxaccept},
  39.   {"check", &rexxcheck},
  40.   {"quickcheck", &rexxquickcheck},
  41.   {"lookup", &rexxlookup},
  42.   {"filecheck", &rexxfilecheck},
  43.   {"version", &rexxversion},
  44.   {"exit", &rexxexit},
  45.   {NULL, NULL}
  46. };
  47.  
  48. SHORT KeepGoing = TRUE;    /* main loop control value */
  49.  
  50. char rootword[BUFSIZ];
  51.  
  52. int wflag = 0;
  53.  
  54. struct RexxMsg *RexxMsg;
  55.  
  56. void servermode()
  57. {
  58.   long rexxbit;
  59.   long returnbits;
  60.  
  61.   rexxbit = upRexxPort ("IRexxSpell", rcl, NULL, &disp);
  62.  
  63.   printf("Entered ARexx server mode!\n");
  64.   printf("Pass commands through port 'IRexxSpell'\n");
  65.   fflush (stdout);
  66.  
  67.   while (KeepGoing)
  68.     {
  69.       returnbits = Wait (rexxbit | SIGBREAKF_CTRL_C);
  70.       if (returnbits & SIGBREAKF_CTRL_C)
  71.     KeepGoing = 0;
  72.       if (returnbits & rexxbit)
  73.     dispRexxPort ();
  74.     }
  75. /*
  76.  *   With Rexx, we need to bring the port down.  You might make this
  77.  *   part of exit() for programs that have multiple paths to exit.
  78.  */
  79.   dnRexxPort ();
  80. }
  81.  
  82.  
  83. /*
  84.  *   Now we get into the actual code necessary for our REXX port; functions
  85.  *   that do the real work.  Note that this program was not structured
  86.  *   particularly nicely for Rexx; I had to write each of these functions.
  87.  *   Many programs have these subroutines already in place; they are called
  88.  *   as part of the event loop.  This progam, however, just has one big
  89.  *   switch statement with different actions . . .
  90.  *
  91.  *   First, our locals.
  92.  */
  93. /*
  94.  *   This is our main dispatch function.
  95.  */
  96. void disp (struct RexxMsg *msg, struct rexxCommandList *dat, char *p)
  97. {
  98.   (*(dat->userdata)) (msg, p);
  99. }
  100.  
  101. /*
  102.  *   This handler adds a word to the `global personal dictionary.'
  103.  *
  104.  *   Now, in English, the word is added to whatever personal
  105.  *   dictionary was found when ISpell was started up in ARexx
  106.  *   server mode. (Under a multiuser system, we might want to
  107.  *   restrict who can add words to this dictionary.  For now
  108.  *   we just do it!  Yes, the AmigaOS is multiuser, hehe :-).
  109.  *   No protection, the way true hackers like it, you (the 
  110.  *   user) get more rope to hang yourself :-), err the system 
  111.  *   with :-).
  112.  */
  113. void rexxadd (struct RexxMsg *msg, char *inword)
  114. {
  115.   if (*inword)
  116.     treeinsert (ichartosstr (strtosichar (inword, 0), 1), ICHARTOSSTR_SIZE, 1);
  117.   treeoutput ();
  118.   replyRexxCmd (msg, 0L, 0L, "ok");
  119. }
  120.  
  121. /*
  122.  *   The quick version does not write the changes to disk.
  123.  *   Useful if you will be adding many words.  Be sure to
  124.  *   call rexxadd at the end of the list (pass a null word,
  125.  *   or a valid new word to be added).
  126.  */
  127. void rexxquickadd (struct RexxMsg *msg, char *inword)
  128. {
  129.   if (*inword)
  130.     treeinsert (ichartosstr (strtosichar (inword, 0), 1), ICHARTOSSTR_SIZE, 1);
  131.   replyRexxCmd (msg, 0L, 0L, "ok");
  132. }
  133.  
  134. /*
  135.  * Accept word for the rest of this session. Obviously there is no need
  136.  * for saving the tree.
  137.  */
  138.  
  139. void rexxaccept (struct RexxMsg *msg, char *inword)
  140. {
  141.   if (*inword)
  142.     treeinsert (ichartosstr (strtosichar (inword, 0), 1), ICHARTOSSTR_SIZE, 0);
  143.   replyRexxCmd (msg, 0L, 0L, "ok");
  144. }
  145.  
  146. /*
  147.  *   This handler checks the spelling of a word.
  148.  */
  149.  
  150. void rexxcheck (struct RexxMsg *msg, char *inword)
  151. {
  152.   char buf[512];
  153.   char aWord[INPUTWORDLEN + MAXAFFIXLEN];
  154.  
  155.   int    i;
  156.  
  157.   for (i=0;i<=strlen(inword);i++){
  158.     ctoken[i]=inword[i];
  159.     itoken[i]=chartoichar(inword[i]);
  160.   }
  161.  
  162.   currentchar = inword;                        /* Global ISpell vars */
  163.  
  164.   if (good (itoken, 0, 0, 0, 0)){
  165.     if (hits[0].prefix == NULL && hits[0].suffix == NULL){
  166.       /* perfect match */
  167.       strcpy (buf, "*");
  168.     } else {
  169.       /* matched because of root */
  170.       sprintf (buf, "+ %s", hits[0].dictent->word);
  171.     }
  172.   } else if (compoundgood (itoken, 0)){
  173.     /* compound-word match */
  174.     strcpy (buf, "-");
  175.   }    else {
  176.     makepossibilities (itoken);
  177.     if (pcount){
  178.       /*
  179.       ** print &  or ?, ctoken, then
  180.       ** character offset, possibility
  181.       ** count, and the possibilities.
  182.       */
  183.       sprintf (buf, "%c %s %d", easypossibilities ? '&' : '?',ctoken, easypossibilities);
  184.       for (i = 0;  i < MAXPOSSIBLE;  i++){
  185.         if (possibilities[i][0] == 0)
  186.           break;
  187.         sprintf (aWord, "%c %s",i ? ',' : ':', possibilities[i]);
  188.       strcat(buf, aWord);
  189.       }
  190.     } else {
  191.       /*
  192.       ** No possibilities found for word TOKEN
  193.       */
  194.       (void) sprintf (buf, "# %s", ctoken);
  195.     }
  196.   }
  197.   replyRexxCmd (msg, 0L, 0L, buf);
  198. }
  199.  
  200.  
  201. /*
  202.  *   This handler checks the spelling of a word, it does
  203.  *   not try to find replacement words as above.  It only
  204.  *   determines whether or not the word is in the dictoinary.
  205.  */
  206. void rexxquickcheck (struct RexxMsg *msg, char *inword)
  207. {
  208.   int    i;
  209.  
  210.   for (i=0;i<=strlen(inword);i++)
  211.     itoken[i]=chartoichar(inword[i]);
  212.  
  213.   currentchar = inword;                        /* Global ISpell vars */
  214.  
  215.   if (good (itoken, 0, 0, 0, 0))
  216.     replyRexxCmd (msg, 0L, 0L, "ok");
  217.   else
  218.     replyRexxCmd (msg, 0L, 0L, "bad");
  219. }
  220.  
  221. /*
  222.  * Find words matching a regular expression
  223.  */
  224. void rexxlookup (struct RexxMsg *msg, char *p)
  225. {
  226.   char buf[512];
  227.   char cmd[512];
  228.   char *rval;
  229.   int whence = 0;
  230.   int bufend = 1;
  231.   int lookupsize;
  232.  
  233.   strcpy (buf, "&");
  234.   sprintf (cmd, "^%s$", p);
  235.   while ((rval = do_regex_lookup (cmd, whence)) != NULL){
  236.     whence=1;
  237.     lookupsize = strlen(rval);
  238.     if ((lookupsize + bufend) > 511)
  239.       break;
  240.     strcpy (&(buf[bufend++]), " ");
  241.     strcpy (&(buf[bufend]), rval);
  242.     bufend += lookupsize;
  243.   }
  244.   replyRexxCmd (msg, 0L, 0L, buf);
  245. }
  246.  
  247. /*
  248.  *   This handler checks the spelling of a file.
  249.  */
  250. void rexxfilecheck (struct RexxMsg *msg, char *p)
  251. {
  252.   char *cp;
  253.  
  254.   currentfile = p;
  255.  
  256.   if ((infile = fopen (p, "r")) == NULL){
  257.     replyRexxCmd (msg, 0L, 0L, "Error: Can't open input file");
  258.     return;
  259.   }
  260.  
  261.   tmpnam (tempfile);
  262.  
  263.   if ((outfile = fopen (tempfile, "w")) == NULL){
  264.     replyRexxCmd (msg, 0L, 0L, "Error: Can't open output file");
  265.     return;
  266.   }
  267.  
  268.   quit = 0;
  269.  
  270.   /* See if the file is a .tex file.  If so, set the appropriate flag. */
  271.   if ((cp = (char *) strrchr (p, '.')) != NULL && strcmp (cp, ".tex") == 0)
  272.     tflag = 1;
  273.   else
  274.     tflag = 0;
  275.  
  276.   lflag = 1;
  277.  
  278.   checkfile ();
  279.  
  280.   fclose (infile);
  281.   fclose (outfile);
  282.  
  283.   replyRexxCmd (msg, 0L, 0L, tempfile);
  284. }
  285.  
  286. /*
  287.  *   This handler returns the version of the program.
  288.  */
  289. void rexxversion (struct RexxMsg *msg, char *p)
  290. {
  291.   char buf[512];
  292.   int bufend;
  293.   int namesize;
  294.   int i;
  295.  
  296.   sprintf(buf, "%s\n%s\n", Version_ID[0], Version_ID[2]);
  297.   bufend=strlen(buf);
  298.   for (i = 0; rcl[i].name != NULL; i++){
  299.     namesize = strlen(rcl[i].name);
  300.     if ((namesize + bufend) > 511)
  301.       break;
  302.     bufend=+namesize+1;
  303.     strcat (buf, " ");
  304.     strcat (buf, rcl[i].name);
  305.   }
  306.   replyRexxCmd (msg, 0L, 0L, buf);
  307. }
  308.  
  309. /*
  310.  *   This handler sets the exit flag.
  311.  */
  312. void rexxexit (struct RexxMsg *msg, char *p)
  313. {
  314.   KeepGoing = 0;
  315.   replyRexxCmd (msg, 0L, 0L, "bye");
  316. }
  317.